<template>
  <el-dialog :title="title" :visible.sync="backShow" append-to-body width="500px">
    <el-form class="dialog-form" label-suffix="：" ref="backForm" :model="form" :rules="isEdit ? rules : {}"
             :label-width="isEdit ? '120px' : '20px'">
      <el-form-item :label="isEdit ? '退回原因' : ''" prop="reason">
        <el-input v-if="isEdit" :maxlength="500" type="textarea" :rows="4" v-model="form.reason"
                  placeholder="请输入退回原因"/>
        <div v-else>{{ reason }}</div>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-if="isEdit" size="small" @click="rejectCancel">取 消</el-button>
      <el-button v-if="isEdit" size="small" type="primary" @click="rejectSubmit">确 定</el-button>
      <el-button v-if="!isEdit" size="small" type="primary" @click="rejectCancel">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {customerReturnPool, pushReturnCustomer} from "@/api/customer/customer";
import {returnCustomer, bathReturnTotalCutomer} from "@/api/customerPool/customerPool"

export default {
  name: 'BackReturn',
  props: {
    isEdit: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      title: '',
      customerId: '',
      currentType: '', // 客户池退回 pool 我的客户退回 myCustomer
      backShow: false,
      form: {
        reason: ''
      },
      rules: {
        reason: [
          {required: true, message: '退回原因不能为空', trigger: 'blur'},
        ],
      },
      customerIds: [],
      reason: '', // 退回原因
    }
  },
  methods: {
    /** 获取退回原因 */
    getReason(customerId) {
      // todo 根据客户查询 退回原因
    },
    /** 打开退回弹框（customerIds 传递的集合（集合中  放的是对象）） */
    openDialog(customerIds, title, currentType) {
      this.reason = '';
      this.form.reason = ''
      this.title = title;
      this.customerIds = customerIds;
      this.currentType = currentType;
      this.backShow = true;
      this.resetForm('backForm');
    },
    /** 取消 */
    rejectCancel() {
      this.backShow = false;
      //if (this.$parent.getList) {
      //  console.log("取消getList刷新了")
      //  this.$parent.getList();
      //}
      //// 星级弹框二次弹框刷新上一个弹框
      //if (this.$parent.$parent.refreshList) {
      //  console.log("二级弹框取消getList刷新了")
      //  this.$parent.$parent.refreshList();
      //}
    },
    /** 确定 */
    rejectSubmit() {
      this.$refs['backForm'].validate((valid) => {
        if (valid) {
          // 客户退回
          if (this.currentType == 'myCustomer' || this.currentType == 'claim') {
            const data = {
              customerVos: this.customerIds,
              backReason: this.form.reason
            }
            customerReturnPool(data).then((res) => {
              if (res.code === 200) {
                this.backShow = false;
                this.$modal.msgSuccess(res.msg);
                if (this.$parent.getList) {
                  console.log("getList刷新了")
                  this.$parent.getList();
                }
                if (this.$parent.$parent.refreshList) {
                  console.log("二级弹框取消getList刷新了")
                  this.$parent.$parent.refreshList();
                }
              } else {
                this.$modal.msgError(res.msg);
              }
            });
            // 池子退回
          } else if (this.currentType == 'pool') {
            const data = {
              customers: this.customerIds,
              backReason: this.form.reason
            }
            returnCustomer(data).then((res) => {
              if (res.code === 200) {
                this.backShow = false;
                this.$modal.msgSuccess(res.msg);
                if (this.$parent.getList) {
                  this.$parent.getList();
                }
                if (this.$parent.$parent.refreshList) {
                  console.log("二级弹框取消getList刷新了")
                  this.$parent.$parent.refreshList();
                }
              } else {
                this.$modal.msgError(res.msg);
              }
            });
          } else if (this.currentType == 'total') {
            const data = {
              ids: this.customerIds,
              remark: this.form.reason
            }
            bathReturnTotalCutomer(data).then((res) => {
              if (res.code === 200) {
                this.backShow = false;
                this.$modal.msgSuccess(res.msg);
                if (this.$parent.getList) {
                  this.$parent.getList();
                }
                if (this.$parent.$parent.refreshList) {
                  console.log("二级弹框取消getList刷新了")
                  this.$parent.$parent.refreshList();
                }
              } else {
                this.$modal.msgError(res.msg);
              }
            });
          } else if (this.currentType == 'pushCustomer') {
            const data = {
              customerVos: this.customerIds,
              backReason: this.form.reason
            }
            pushReturnCustomer(data).then((res) => {
              if (res.code === 200) {
                this.backShow = false;
                this.$modal.msgSuccess(res.msg);
                if (this.$parent.getList) {
                  this.$parent.getList();
                }
                if (this.$parent.$parent.refreshList) {
                  console.log("二级弹框取消getList刷新了")
                  this.$parent.$parent.refreshList();
                }
              } else {
                this.$modal.msgError(res.msg);
              }
            });
          }
        }
      })
    },
  }
}
</script>

<style scoped lang="scss">

</style>
